<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\Admin\Auth\AdminAuth;
use App\Http\Controllers\Business\Admin\Common\Common;
use App\Http\Controllers\Business\Shop\V1\Admin\Admin;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Log;

class AdminController extends WebController
{
    //后台账号信息列表
    public function accountList() {
        $cityId = $this->request->get('cityid');
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        $length = 1000;
        $result = AdminAuth::adminUserList($cityId, $roleId, $length);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'lists' => $lists,
            'cityid' => $cityId,
            'roleid' => $roleId,
        ];
        return view('console.admin.account_list', $data);
    }
    //获取帐号信息
    public function getAccount() {
        $adminUserId = $this->request->get('adminuserid');
        $cityId = $this->request->get('cityid');
        $result = AdminAuth::getAdminUser($adminUserId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        $roleListResult = AdminAuth::getSubRoleListByRoleId($roleId);
        if($roleListResult['status'] == 'failed') {
            $roleList = [];
        } else {
            $roleList = $roleListResult['data'];
        }
        $cityListResult = AdminAuth::getCityListByRoleIdAndCityId($roleId, $cityId);
        if($cityListResult['status'] == 'failed') {
            $cityList = [];
        } else {
            $cityList = $cityListResult['data'];
        }
        $data['rolelist'] = $roleList;
        $data['cityid'] = $cityId;
        $data['citylist'] = $cityList;
        $cityAuthArray = [];
        if(isset($data['cityauth'])) {
            $cityAuth = json_decode($data['cityauth'], true);
            $cityAuthResult = Common::getAreaByCityAuth($cityAuth);
            if($cityAuthResult['status'] == 'success') {
                $cityAuthArray = $cityAuthResult['data'];
            }
        }
        $data['cityautharray'] = $cityAuthArray;
        return view('console.admin.account_detail', $data);
    }
    //新增帐号
    public function newAccount() {
        $cityId = $this->request->get('cityid');
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        $roleListResult = AdminAuth::getSubRoleListByRoleId($roleId);
        if($roleListResult['status'] == 'failed') {
            $roleList = [];
        } else {
            $roleList = $roleListResult['data'];
        }
        $cityListResult = AdminAuth::getCityListByRoleIdAndCityId($roleId, $cityId);
        if($cityListResult['status'] == 'failed') {
            $cityList = [];
        } else {
            $cityList = $cityListResult['data'];
        }
        $data = [
            'roleid' => $roleId,
            'rolelist' => $roleList,
            'cityid' => $cityId,
            'citylist' => $cityList,
        ];
        return view('console.admin.new_account', $data);
    }
    //提交新增数据
    public function addAccount() {
        $input = $this->request->input();
        $mobile = $input['mobile'];
        $password = $input['password'];
        $realName = $input['realname'];
        $roleId = $input['roleid'];
        $isAllData = $input['isalldata'];
        $status = $input['status'];
        $note = $input['note'];
        $adminCityString = $input['admincitystring'];
        $districtId = '';
        $cityId = '';
        $cityArray = json_decode($adminCityString, true);
        foreach($cityArray as $key=>$value) {
            if($cityId) {
                $cityId .= ',' . $key;
            } else {
                $cityId = $key;
            }
            foreach($value as $list) {
                if ($districtId) {
                    $districtId .= ',' . $list;
                } else {
                    $districtId .= $list;
                }
            }
        }
        $cityAuth = json_encode($cityArray);
        $result = AdminAuth::addAdminUser($mobile,$password,$realName,$roleId,$isAllData,$status,$cityAuth,$note,$cityId,$districtId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['message']);
    }
    //编辑信息
    public function editAccount() {
        $input = $this->request->input();
        $adminUserId = $input['adminuserid'];
        $realName = $input['realname'];
        $roleId = $input['roleid'];
        $isAllData = $input['isalldata'];
        $status = $input['status'];
        $cityAuth = $input['admincitystring'];
        $note = $input['note'];
        $resignTime = $input['resigntime'];
        $result = AdminAuth::updateAdminUser($adminUserId,$realName,$roleId,$isAllData,$status,$cityAuth,$note, $resignTime);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //修改帐号昵称
    public function updateAccountMobile() {
        $userId = Auth::guard('adminweb')->id();
        $oldMobile = $this->request->get('oldmobile');
        $newMobile = $this->request->get('newmobile');
        $result = AdminAuth::updateMobile($userId, $oldMobile, $newMobile);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //修改帐号密码
    public function updateAccountPassword() {
        $adminUserId = $this->request->get('adminuserid');
        $password = $this->request->get('password');
        $result = AdminAuth::updatePassword($adminUserId, $password);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //删除帐号
    public function deleteAccount() {

    }
    //解禁，禁用帐号
    public function updateAccountStatus() {
        $accountId = $this->request->get('accountid');
        $cityId = $this->request->get('cityid');
        $status = $this->request->get('status');
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        $result = AdminAuth::updateAdminUserStatus($roleId, $accountId, $status);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //导出帐号
    public function exportAccount() {

    }
    //后台角色管理
    public function roleManagement() {
        $cityId = $this->request->get('cityid');
        $result = AdminAuth::adminRoleList();
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.admin.role_management', $data);
    }
    //后台新增角色
    public function addRole() {
        $cityId = $this->request->get('cityid');
        $lists = AdminAuth::getAllAuth();
        $data = [
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.admin.new_role', $data);
    }
    //后台获取角色
    public function getRole() {
        $cityId = $this->request->get('cityid');
        $roleId = $this->request->get('roleid');
        $result = AdminAuth::getRoleInfoByRoleId($roleId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data =  $result['data'];
        $data['cityid'] = $cityId;
        return view('console.admin.role_check', $data);
    }
    //编辑，新增角色
    public function editRole() {
        $roleName = $this->request->get('rolename');
        $auth = $this->request->get('auth');
        $action = $this->request->get('action');
        if($action == 'add') {
            $result = AdminAuth::addAdminRole($roleName, $auth);
        } elseif($action == 'edit') {
            $roleId = $this->request->get('roleid');
            $result = AdminAuth::updateAdminRole($roleId, $roleName, $auth);
        } else {
            return $this->responseError("参数错误");
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            //刷新权限
            AdminAuth::refreshAdminRoleAuthByRoleName($roleName);
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //后台删除角色
    public function deleteRole() {
        $cityId = $this->request->get('cityid');
        $roleId = $this->request->get('roleid');
        //TODO 处理等待
        return $this->responseSuccessMessage("删除成功");
    }
    //获取所有的商务人员的名称
    public function getAllByRole() {
        $cityId = $this->request->get('cityid');
        $result = AdminAuth::getBusinessPeopleList($cityId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //刷新权限
    public function refreshAdminUserAuth() {
        if($this->request->has('adminuserid')) {
            $adminUserId = $this->request->get('adminuserid');
        } else {
            $adminUserId = '';
        }
        $result = AdminAuth::refreshAdminUserAuth($adminUserId);
        return $this->responseSuccessMessage($result['message']);
    }
    //通过类型来判定登录用户是否具备该类型的权限
    public function isValidAuthByType() {
        $type = $this->request->get('type');
        $adminUserId = Auth::guard('adminweb')->id();
        if(AdminAuth::isHasThisAuthByAdminUserId($adminUserId, $type)) {
            return $this->responseSuccessMessage('具备该权限');
        } else {
            return $this->responseError('不具备该权限');
        }
    }
    //判断登录用户是否具备短信验证码的权限
    public function isSmsValidAuth() {
        $auth = 'smsSelect';
        $adminUserId = Auth::guard('adminweb')->id();
        if(AdminAuth::isHasThisAuthByAdminUserId($adminUserId, $auth)) {
            return $this->responseSuccessMessage('可以查询');
        } else {
            return $this->responseError('不可以查询');
        }
    }

    //后台代理商管理列表
    public function agentList() {
        $cityId = $this->request->get('cityid');
        $result = AdminAuth::agentList();
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.admin.agent_list', $data);
    }
    //后台代理商用户新增
    public function addAgent() {
        $cityId = $this->request->get('cityid');
        $cityListResult = AdminAuth::getCityListByRoleIdAndCityId(1, $cityId);
        if($cityListResult['status'] == 'failed') {
            $cityList = [];
        } else {
            $cityList = $cityListResult['data'];
        }
        $data = [
            'cityid' => $cityId,
            'citylist' => $cityList,

        ];
        return view('console.admin.agent_new', $data);
    }
    //后台代理商获取角色
    public function getAgent() {
        $adminUserId = $this->request->get('adminuserid');
        $cityId = $this->request->get('cityid');
        $cityListResult = AdminAuth::getCityListByRoleIdAndCityId(1, $cityId);
        if($cityListResult['status'] == 'failed') {
            $cityList = [];
        } else {
            $cityList = $cityListResult['data'];
        }
        $result = AdminAuth::getAgent($adminUserId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        $data['citylist'] = $cityList;
        $cityAuthArray = [];
        if(isset($data['cityauth'])) {
            $cityAuth = json_decode($data['cityauth'], true);
            $cityAuthResult = Common::getAreaByCityAuth($cityAuth);
            if($cityAuthResult['status'] == 'success') {
                $cityAuthArray = $cityAuthResult['data'];
            }
        }
        $data['cityautharray'] = $cityAuthArray;
        return view('console.admin.agent_check', $data);
    }
    //后台代理商编辑，新增
    public function editAgent() {
        $action = $this->request->get('action');
        $mobile = $this->request->get('mobile');
        $realName = $this->request->get('realname');
        $status = $this->request->get('status');
        $note = $this->request->get('note');
        $adminCityString = $this->request->get('admincitystring');
        $districtId = '';
        $cityId = '';
        $cityArray = json_decode($adminCityString, true);
        foreach($cityArray as $key=>$value) {
            if($cityId) {
                $cityId .= ',' . $key;
            } else {
                $cityId = $key;
            }
            foreach($value as $list) {
                if ($districtId) {
                    $districtId .= ',' . $list;
                } else {
                    $districtId .= $list;
                }
            }
        }
        $cityAuth = json_encode($cityArray);
        if($action == 'add') {
            $password = $this->request->get('password');
            $cardHolder = $this->request->get('cardholder');
            $bankCard = $this->request->get('bankcard');
            $bankName = $this->request->get('bankname');
            $bankProvince = $this->request->get('bankprovince');
            $bankCity = $this->request->get('bankcity');
            $openCardBank = $this->request->get('opencardbank');
            $result = AdminAuth::addAgent($mobile,$password,$realName,$status,$cityAuth,$note,$cityId,$districtId, $cardHolder, $bankCard,$bankName, $bankProvince, $bankCity, $openCardBank);
        } elseif($action == 'edit') {
            $result = AdminAuth::editAgent();
        } else {
            return $this->responseError('参数错误');
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
}